package com.gzz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzz.mapper.ImgsRespMapper;
import com.gzz.pojo.Food;
import com.gzz.mapper.FoodMapper;
import com.gzz.pojo.ImgsResp;
import com.gzz.service.FoodService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzz.vo.FoodVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.gzz.utils.OssUtils.FoodType;
import static com.gzz.utils.OssUtils.GoodTypeId;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 周佳成
 * @since 2023-11-27
 */
@Service
@Slf4j
public class FoodServiceImpl extends ServiceImpl<FoodMapper, Food> implements FoodService {
    @Resource
    FoodMapper foodMapper;
    @Resource
    ImgsRespMapper imgsRespMapper;
    @Override
    public List<Food> getFindList(Page<Food> foodPage, QueryWrapper<Food> queryWrapper) {
         foodMapper.selectPage(foodPage,queryWrapper);
         List<Food> foodList = foodPage.getRecords();
        return foodList;
    }

    @Override
    public List<FoodVo> getImage(List<Food> foodList) {
        List<FoodVo> foodVoList = new ArrayList<>();
        log.info("到这了");
         for(Food food:foodList)
        {
            FoodVo foodVo = new FoodVo();
            BeanUtils.copyProperties(food,foodVo);
            QueryWrapper<ImgsResp> imgsRespQueryWrapper = new QueryWrapper<>();
            imgsRespQueryWrapper.eq("st_id",food.getFoodId())
                    .eq("type_id",FoodType);
            List<ImgsResp> imgsRespList = imgsRespMapper.selectList(imgsRespQueryWrapper);
            List<String> stringList = new ArrayList<>();
            for(ImgsResp imgsResp : imgsRespList)
            {
                stringList.add(imgsResp.getImageUrl());
            }
            foodVo.setImgs(stringList);
            foodVoList.add(foodVo);
        }
         log.info("内容华为"+ foodVoList);
        return foodVoList;
    }
    @Override
    public int removeByIdAndTypeId(Long stId, Integer strategyTypeId) {
        //通过id,和typeId删除数据库的图片路径
        LambdaQueryWrapper<ImgsResp> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ImgsResp::getTypeId,FoodType).eq(ImgsResp::getStId,stId);

        int  r = imgsRespMapper.delete(lambdaQueryWrapper);
        return  r;
    }

    @Override
    public void saveVo(FoodVo foodVo) {
        Food food = new Food();
        BeanUtils.copyProperties(foodVo,food);
        if(foodVo.getImgs()!=null)
        for(String url: foodVo.getImgs())
        {
            ImgsResp imgsResp = new ImgsResp();
            imgsResp.setImageUrl(url);
            imgsResp.setStId(foodVo.getFoodId());
            imgsResp.setTypeId(FoodType);
            imgsRespMapper.insert(imgsResp);
        }
        foodMapper.insert(food);
    }
}
